﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = "Index";
}

<label>shop name</label>
<span data-bind="text:ShopName"></span>
<!-- ko foreach: ProductList -->
    @Html.Partial("ProductList.SingleProduct")
 <!-- /ko -->


@{
    var model = ViewBag.Model;

    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

    @Scripts.Render("~/scripts/Product/Product.js")

@section scripts
{
    <script>
        var init = @jsonStr;

        function ShopModel() {
            var self = this;
            self.ShopName = ko.observable(init.ShopName);
            var tempProducts = $.isEmptyObject(init.ProductList) ? [] : $.map(init.ProductList, function(a) {
                var picModel = new PictureModel(a.DefaultPictureModel.ImageUrl, a.DefaultPictureModel.FullSizeImageUrl, a.DefaultPictureModel.Title);
                var tempSpecAttributes= $.isEmptyObject(a.SpecificationAttributeModels) ? [] : $.map(a.SpecificationAttributeModels, function(c) {
                    return new ProductSpecificationAttributeModel(c.AttributeName, c.AttributeOptionName);
                });
                return new ProductOverviewModel(a.Id, a.Name, a.ShortDescription, a.FullDescription, a.MinPrice, picModel, tempSpecAttributes);
            });

            self.ProductList = ko.observableArray(tempProducts);
        }


        var vm = new ShopModel();
        ko.applyBindings(vm);
    </script>

}
